package com.tiennt.android.quiethour;

import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.widget.TimePicker;

public class QuietHourActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {
	/** Called when the activity is first created. */
	private int mStartHour;
	private int mStartMinute;
	private int mEndHour;
	private int mEndMinute;
	private Preference mStartQuietHourPref;
	private Preference mEndQuietHourPref;

	private CheckBoxPreference mEnableQuietHoursPref;
	private CheckBoxPreference mMuteNotificationsPref;
	private CheckBoxPreference mDisableHapticFeedbackPref;
	private CheckBoxPreference mDisableVibrationsPref;
	private CheckBoxPreference mDimLEDPref;

	public QuietHourActivity() {

	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.xml.preferences);

		init();
	}

	private void init() {

		mStartQuietHourPref = (Preference) findPreference(QuietHoursUtils.KEY_START_QUIETHOURS_PREF);
		mEndQuietHourPref = (Preference) findPreference(QuietHoursUtils.KEY_END_QUIETHOURS_PREF);

		mEnableQuietHoursPref = (CheckBoxPreference) findPreference(QuietHoursUtils.KEY_ENABLE_QUIETHOURS_PREF);
		mMuteNotificationsPref = (CheckBoxPreference) findPreference(QuietHoursUtils.KEY_MUTE_NOTIFICATIONS_PREF);
		mDisableHapticFeedbackPref = (CheckBoxPreference) findPreference(QuietHoursUtils.KEY_DISABLE_HAPTIC_FEEDBACK_PREF);
		mDisableVibrationsPref = (CheckBoxPreference) findPreference(QuietHoursUtils.KEY_DISABLE_VIBRATIONS_PREF);
		mDimLEDPref = (CheckBoxPreference) findPreference(QuietHoursUtils.KEY_DIM_LED_PREF);

		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(this);

		mStartHour = sharedPreferences
				.getInt(QuietHoursUtils.KEY_START_HOUR, 0);
		mStartMinute = sharedPreferences.getInt(
				QuietHoursUtils.KEY_START_MINUTE, 0);
		updateTime(mStartQuietHourPref, mStartHour, mStartMinute);

		mEndHour = sharedPreferences.getInt(QuietHoursUtils.KEY_END_HOUR, 0);
		mEndMinute = sharedPreferences
				.getInt(QuietHoursUtils.KEY_END_MINUTE, 0);
		updateTime(mEndQuietHourPref, mEndHour, mEndMinute);

		mStartQuietHourPref
				.setOnPreferenceClickListener(onPreferenceClickListener);
		mEndQuietHourPref
				.setOnPreferenceClickListener(onPreferenceClickListener);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		getPreferenceScreen().getSharedPreferences()
				.unregisterOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(this);
		mMuteNotificationsPref
				.setSummary(sharedPreferences.getBoolean(
						QuietHoursUtils.KEY_MUTE_NOTIFICATIONS_PREF, false) == true ? getResources()
						.getString(R.string.muteNotifications_checked_sum)
						: getResources().getString(
								R.string.muteNotifications_unchecked_sum));
		mDisableHapticFeedbackPref
				.setSummary(sharedPreferences
						.getBoolean(
								QuietHoursUtils.KEY_DISABLE_HAPTIC_FEEDBACK_PREF,
								false) == true ? getResources().getString(
						R.string.disableHapticFeedback_checked_sum)
						: getResources().getString(
								R.string.disableHapticFeedback_unchecked_sum));
		mDisableVibrationsPref
				.setSummary(sharedPreferences.getBoolean(
						QuietHoursUtils.KEY_DISABLE_VIBRATIONS_PREF, false) == true ? getResources()
						.getString(R.string.disableVibrations_checked_sum)
						: getResources().getString(
								R.string.disableVibrations_unchecked_sum));
		mDimLEDPref
				.setSummary(sharedPreferences.getBoolean(
						QuietHoursUtils.KEY_DIM_LED_PREF, false) == true ? getResources()
						.getString(R.string.dimLed_checked_sum)
						: getResources().getString(
								R.string.dimLed_unchecked_sum));
		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);

	}

	OnTimeSetListener onStartTimeSet = new OnTimeSetListener() {

		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			// TODO Auto-generated method stub
			SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(QuietHourActivity.this);
			SharedPreferences.Editor editor = pref.edit();
			mStartHour = hourOfDay;
			mStartMinute = minute;
			editor.putInt(QuietHoursUtils.KEY_START_HOUR, hourOfDay);
			editor.putInt(QuietHoursUtils.KEY_START_MINUTE, minute);
			editor.commit();
			updateTime(mStartQuietHourPref, hourOfDay, minute);
		}
	};

	OnTimeSetListener onEndTimeSet = new OnTimeSetListener() {

		public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
			// TODO Auto-generated method stub
			SharedPreferences pref = PreferenceManager
					.getDefaultSharedPreferences(QuietHourActivity.this);
			SharedPreferences.Editor editor = pref.edit();
			mEndHour = hourOfDay;
			mEndMinute = minute;
			editor.putInt(QuietHoursUtils.KEY_END_HOUR, hourOfDay);
			editor.putInt(QuietHoursUtils.KEY_END_MINUTE, minute);
			editor.commit();

			updateTime(mEndQuietHourPref, hourOfDay, minute);
		}
	};

	public void updateTime(Preference pref, int hour, int minute) {
		String time = "";
		time += ((hour < 10) ? "0" + hour : hour) + " : "
				+ ((minute < 10) ? "0" + minute : minute);
		pref.setSummary(time);
	}

	OnPreferenceClickListener onPreferenceClickListener = new OnPreferenceClickListener() {

		public boolean onPreferenceClick(Preference preference) {
			// TODO Auto-generated method stub
			if (preference == mStartQuietHourPref) {
				TimePickerDialog timePicker = new TimePickerDialog(
						QuietHourActivity.this, onStartTimeSet, mStartHour,
						mStartMinute, true);
				timePicker.show();
			} else if (preference == mEndQuietHourPref) {
				TimePickerDialog timePicker = new TimePickerDialog(
						QuietHourActivity.this, onEndTimeSet, mEndHour,
						mEndMinute, true);
				timePicker.show();
			}
			return true;
		}
	};

	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		// TODO Auto-generated method stub
		if (key.equals(QuietHoursUtils.KEY_ENABLE_QUIETHOURS_PREF)) {
			mEnableQuietHoursPref.setSummary(sharedPreferences.getBoolean(key,
					false) == true ? getResources().getString(
					R.string.enableQuietHour_checked_sum) : getResources()
					.getString(R.string.enableQuietHour_unchecked_sum));
		} else if (key.equals(QuietHoursUtils.KEY_MUTE_NOTIFICATIONS_PREF)) {
			mMuteNotificationsPref
					.setSummary(sharedPreferences.getBoolean(
							QuietHoursUtils.KEY_MUTE_NOTIFICATIONS_PREF, false) == true ? getResources()
							.getString(R.string.muteNotifications_checked_sum)
							: getResources().getString(
									R.string.muteNotifications_unchecked_sum));

		} else if (key.equals(QuietHoursUtils.KEY_DISABLE_HAPTIC_FEEDBACK_PREF)) {
			mDisableHapticFeedbackPref
					.setSummary(sharedPreferences.getBoolean(
							QuietHoursUtils.KEY_DISABLE_HAPTIC_FEEDBACK_PREF,
							false) == true ? getResources().getString(
							R.string.disableHapticFeedback_checked_sum)
							: getResources()
									.getString(
											R.string.disableHapticFeedback_unchecked_sum));
		} else if (key.equals(QuietHoursUtils.KEY_DISABLE_VIBRATIONS_PREF)) {
			mDisableVibrationsPref
					.setSummary(sharedPreferences.getBoolean(
							QuietHoursUtils.KEY_DISABLE_VIBRATIONS_PREF, false) == true ? getResources()
							.getString(R.string.disableVibrations_checked_sum)
							: getResources().getString(
									R.string.disableVibrations_unchecked_sum));
		} else if (key.equals(QuietHoursUtils.KEY_DIM_LED_PREF)) {
			mDimLEDPref
					.setSummary(sharedPreferences.getBoolean(
							QuietHoursUtils.KEY_DIM_LED_PREF, false) == true ? getResources()
							.getString(R.string.dimLed_checked_sum)
							: getResources().getString(
									R.string.dimLed_unchecked_sum));
		}
	}
}
